package com.zb.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Assert;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zb.constant.FieldTypeConstant;
import com.zb.entity.*;
import com.zb.mapper.ZjGoodsFieldMapper;
import com.zb.result.PageUtil;
import com.zb.service.ZjGoodsFieldContentService;
import com.zb.service.ZjGoodsFieldService;
import com.zb.service.ZjProductParamFieldValService;
import com.zb.service.ZjSpareParamFieldValService;
import com.zb.vo.GoodsFieldContentVo;
import com.zb.vo.GoodsFieldListVo;
import com.zb.vo.GoodsFieldVo;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 产品字段表 服务实现类
 * </p>
 *
 * @author WEN
 * @since 2023-08-01
 */
@Service
public class ZjGoodsFieldServiceImpl extends ServiceImpl<ZjGoodsFieldMapper, ZjGoodsField> implements ZjGoodsFieldService {
    @Resource
    private ZjGoodsFieldMapper goodsFieldMapper;
    @Resource
    private ZjGoodsFieldContentService goodsFieldContentService;
    @Resource
    private ZjProductParamFieldValService productParamFieldValService;
    @Resource
    private ZjSpareParamFieldValService spareParamFieldValService;

    // 新增或修改商品字段
    @Override
    @Transactional
    public void save(Long custAccId, GoodsFieldVo goodsFieldVo) {
        Assert.isTrue(null == goodsFieldMapper.existByName(custAccId, goodsFieldVo.getId(), goodsFieldVo.getName()), "字段名称已重复！", goodsFieldVo.getName());
        if (goodsFieldVo.getType().equals(FieldTypeConstant.FIELD_TYPE_3) || goodsFieldVo.getType().equals(FieldTypeConstant.FIELD_TYPE_4)) {
            Assert.isTrue(!goodsFieldVo.getContentVos().isEmpty(), "字段类型为'单选/多选'的字段内容至少填写一项", null);
        }

        ZjGoodsField goodsField = new ZjGoodsField();
        BeanUtils.copyProperties(goodsFieldVo, goodsField);
        goodsField.setDisabled(goodsFieldVo.getStatus());
        if (null != goodsField.getId()) {
            this.updateById(goodsField);
            if (null != goodsFieldVo.getContentIds() && !goodsFieldVo.getContentIds().isEmpty()) {
                goodsFieldContentService.removeByIds(goodsFieldVo.getContentIds());
            }
            goodsFieldContentService.save(custAccId, goodsField.getId(), goodsFieldVo.getContentVos());
            return;
        }
        goodsField.setId(IdWorker.getId());
        goodsField.setCustAccId(custAccId);
        this.save(goodsField);
        goodsFieldContentService.save(custAccId, goodsField.getId(), goodsFieldVo.getContentVos());
    }

    // 获取商品字段列表
    @Override
    public Map<String, Object> getList(Long custAccId, FieldSelectVo selectVo) {
        QueryWrapper<ZjGoodsField> wrapper = new QueryWrapper<>();
        wrapper.eq("cust_acc_id", custAccId);
        wrapper.orderByDesc("gmt_update");
        if (null != selectVo.getType()) {
            wrapper.eq("type", selectVo.getType());
        }
        if (StringUtils.isNotBlank(selectVo.getName())) {
            wrapper.like("name", selectVo.getName());
        }
        PageUtil.setPageLimit(selectVo);
        Page<ZjGoodsField> page = this.page(new Page<>(selectVo.getPage(), selectVo.getLimit()), wrapper);

        List<Long> fieldIdList = page.getRecords().stream().filter(ZjGoodsField -> ZjGoodsField.getType().equals(FieldTypeConstant.FIELD_TYPE_3)
                || ZjGoodsField.getType().equals(FieldTypeConstant.FIELD_TYPE_4)).map(ZjGoodsField::getId).collect(Collectors.toList());

        List<ZjGoodsFieldContent> contentList = goodsFieldContentService.getByFieldIds(fieldIdList);

        List<GoodsFieldListVo> voList = new ArrayList<>();
        page.getRecords().forEach(t -> {
            voList.add(GoodsFieldListVo.beanToVo(t, contentList.stream().filter(ZjGoodsFieldContent -> ZjGoodsFieldContent.getGoodsFieldId().equals(t.getId())).map(ZjGoodsFieldContent::getVal).collect(Collectors.joining("、"))));
        });
        return PageUtil.pageList(page.getCurrent(), page.getSize(), page.getTotal(), voList);
    }

    // 获取商品字段信息
    @Override
    public GoodsFieldVo getInfo(Long id) {
        ZjGoodsField goodsField = this.getById(id);
        GoodsFieldVo vo = new GoodsFieldVo();
        BeanUtils.copyProperties(goodsField, vo);
        vo.setStatus(goodsField.getDisabled());

        List<ZjGoodsFieldContent> contentList = goodsFieldContentService.getByFieldId(id);
        List<GoodsFieldContentVo> contentVos = new ArrayList<>();

        for (ZjGoodsFieldContent content : contentList) {
            contentVos.add(new GoodsFieldContentVo(content.getId(), content.getVal()));
        }
        vo.setContentVos(contentVos);
        return vo;
    }

    // 批量禁用或启用商品字段
    @Override
    public void updateBatchDisableByIds(List<Long> idList, boolean status) {
        goodsFieldMapper.updateBatchDisableByIds(idList, status);
    }

    // 批量删除商品字段
    @Override
    @Transactional
    public void deleteByIds(List<Long> idList) {
        // todo 查询存在关联的字段
//        goodsFieldContentService.deleteByGoodsFieldIds(idList);
//        this.removeByIds(idList);
    }
}
